package com.example.demo.controllers;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.dto.UserDto;
import com.example.demo.exceptions.UserNotFoundException;
import com.example.demo.models.User;
import com.example.demo.repositorys.UserRepository;
import com.example.demo.services.UserDaoService;
import com.example.demo.services.myServices;

import jakarta.validation.Valid;

@RestController
@RequestMapping("/test")
public class myController {
    @Autowired
    private myServices myServices;

    @Autowired
    private UserDaoService userDaoService;

    @Autowired
    private UserRepository userRepository;

    @GetMapping()
    public String test() {
        throw new UserNotFoundException("错误信息");
        // return "Hello World";
    }

    @GetMapping("/path/{name}")
    public String testPathName(@PathVariable String name) {
        return name;
    }

    @GetMapping("all")
    public ArrayList<UserDto> getAll() {
        return userDaoService.findAll();
    }

    @PostMapping("create")
    public UserDto createUser(@Valid @RequestBody UserDto userDto) {
        return userDaoService.save(userDto);
    }

    @GetMapping("/jpa")
    public List<User> getJpaAll() {
        return userRepository.findAll();
    }

    @PostMapping("/jpa")
    public List<User> savaJpaAll() {
        return userRepository.findAll();
    }
}
